<?php
class Image extends MX_Controller{
	function __construct(){
		parent::__construct();
		if($this->session->userdata('Image')){
			@unlink('data/temp/' . $this->session->userdata('Image'));
		}
	}
	
	function index(){
		$FileName = preg_replace('/[^A-za-z0-9._]/', '', $_FILES['Avatar']['name']);
		$tempFile = 'data/temp/' . $FileName;
		$target = $this->config->item('data') . '/temp/' . $FileName;
		if(($_FILES['Avatar']['type'] != 'image/jpg' 
		and $_FILES['Avatar']['type'] != 'image/jpeg'
		and $_FILES['Avatar']['type'] != 'image/gif')
		or $_FILES['Avatar']['size'] > (1024 * 500))
		{
			echo '<script>alert("File không đúng định dạng hoặc quá lớn")</script>';
		} else {
			if(move_uploaded_file($_FILES['Avatar']['tmp_name'], $tempFile)){
				$this->session->set_userdata('Image', $FileName);
				echo '<script>parent.parent.$(".details .avatar").attr("src", "' . $target . '")</script>';
			} else {
				echo '<script>alert("Không thể tải file. Vui lòng thử lại sau ít phút")</script>';
			}
		}
	}
}
?>